// 初始化google
var myLatlng = {
	lat: -25.363,
	lng: 131.044
};
// 展示牛逼高大上的地图背景类型
var map = new google.maps.Map(document.getElementById('map'), {
	zoom: 2,
	scrollwheel: false,
	center: myLatlng,
	styles: [{
		featureType: "landscape",
		stylers: [{
			hue: "#F1FF00"
		}, {
			saturation: -27.4
		}, {
			lightness: 9.4
		}, {
			gamma: 1
		}]
	}, {
		featureType: "road.highway",
		stylers: [{
			hue: "#0099FF"
		}, {
			saturation: -20
		}, {
			lightness: 36.4
		}, {
			gamma: 1
		}]
	}, {
		featureType: "road.arterial",
		stylers: [{
			hue: "#00FF4F"
		}, {
			saturation: 0
		}, {
			lightness: 0
		}, {
			gamma: 1
		}]
	}, {
		featureType: "road.local",
		stylers: [{
			hue: "#FFB300"
		}, {
			saturation: -38
		}, {
			lightness: 11.2
		}, {
			gamma: 1
		}]
	}, {
		featureType: "water",
		stylers: [{
			hue: "#00B6FF"
		}, {
			saturation: 4.2
		}, {
			lightness: -63.4
		}, {
			gamma: 1
		}]
	}, {
		featureType: "poi",
		stylers: [{
			hue: "#9FFF00"
		}, {
			saturation: 0
		}, {
			lightness: 0
		}, {
			gamma: 1
		}]
	}]
});
var markers = [];
var markerCluster = new MarkerClusterer(map, markers, {
	imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
var locations = [];
// 切换城市请求城市图标数据
function show_map(country, city) {
	// var path = get_path_params(country, city);
	$.ajax({
		url: URL + 'home/ajax_poi_list/' + country+'/'+city,
		type: 'GET',
		dataType: 'json',
		success: function(data) {
			// 更换地图
			var locations = []
			for (var i = 0; i < data.content.data.length; i++) {
				var obj = {}
				var lat = JSON.parse(data.content.data[i].latitude)
				var lng = JSON.parse(data.content.data[i].longitude)
				obj.lat = lat
				obj.lng = lng
				if (obj.lat && obj.lng) {
					locations.push(obj)
				}
				
			}
			var markers = locations.map(function(locations, i) {
				return new google.maps.Marker({
					position: locations,
					map: map
				});
			});
			// console.log(markers);
			markerCluster.clearMarkers();
			markerCluster.addMarkers(markers);

			

			for (var i in markers) {
				var dataz = data.content.data[i]
				// console.log(dataz)
				var type = dataz.type
				var ID = dataz.id
				var name =dataz.name
				var cover =dataz.cover
				// console.log(cover+name)
				// 显示信息弹窗
				var lastInfWnd = null;
				

				if (type == 'touroute') {
					var contentString = '<div id="map_Con"><a href="'+URL +'/touroute/detail/'+ID+'"><img src="'+cover+'" ><p>'+name+'</p></a></div>'
				}else{
					var contentString = '<div id="map_Con"><a href="'+URL +'/merchant/detail/'+ID+'"><img src="'+cover+'" ><p>'+name+'</p></a></div>'

				}

				let infowindow = new google.maps.InfoWindow({
					content: contentString,
					// maxWidth:300,
				});
				let marker = markers[i];
				marker.addListener('click', function(){
					infowindow.open(map, this);
					// 关闭上一个infowindow
					if (lastInfWnd) {
						setTimeout(function() {
							lastInfWnd.close();
						}, 0);
					}
					setTimeout(function() {
						lastInfWnd = infowindow;
					}, 100);

				});
			}


		}
	});
}
show_map(0,0)
// 切换城市自动聚焦城市
function panToCity(cityname) {
	var geocoder = new google.maps.Geocoder;
	geocoder.geocode({
		address: cityname
	}, function(results, status) {
		// console.log(results, status);
		if (status === "OK") {
			if (results[0]) {
				// console.log(results[0]['geometry']['bounds']);
				var place = results[0];
				var geometry = place.geometry;
				var place_id = place.place_id;
				var bounds = geometry.bounds;
				var location = geometry.location;
				var viewport = geometry.viewport;
				// map.createMarker(place);
				map.setCenter(location);
				map.panTo(location);
				// map.fitBounds(bounds); // 完全包含, 大
				map.fitBounds(viewport); // 推荐视口, 小
			} else {
				// console.log("No results found");
			}
		} else {
			// console.log("Geocoder failed due to: " + status);
		}
	});
}